local Msg = require("core.Msg")
local InvestConfig = require("excel.Fuli").invest
local FuliDefine = require("fuli.FuliDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local ShopHelp = require("shop.ShopHelp")
local ObjHuman = require("core.ObjHuman")
local Handler = require("fuli.HandlerL")
local Log = require("common.Log")

--检查是否可领取奖励
function checkShowTips(human)
  for i=1, #InvestConfig do
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + InvestConfig[i].index - 1] == FuliDefine.REWARD_CAN_GET then
      return 1
    end
  end
  return 0
end
--奖励列表
function getRewardList(human)
  changeStatus(human)
  local sendMsg = Msg.gc.GC_FULI_REWARD
  local cache = {}
  sendMsg.list[0] = #InvestConfig
  for i=1, #InvestConfig do
    sendMsg.list[i].page = FuliDefine.FULI_TYPE_INVEST
    sendMsg.list[i].canGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + InvestConfig[i].index - 1]
    sendMsg.list[i].vipCanGet = 0
    sendMsg.list[i].condition = InvestConfig[i].needLv
    sendMsg.list[i].needYuanbao = InvestConfig[i].needYuanbao
    sendMsg.list[i].reward[0] = #InvestConfig[i].reward
    for j=1, #InvestConfig[i].reward do
      Grid.create(cache, InvestConfig[i].reward[j][1], InvestConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end    
    sendMsg.list[i].vipReward[0] = 0 
    sendMsg.list[i].onlineTime = human.db.investRMB
    sendMsg.list[i].percent = InvestConfig[i].percent
  end
  Msg.send(sendMsg, human.fd)
end
--领取奖励
function getReward(human, condition)
  for i=1, #InvestConfig do
    if InvestConfig[i].needYuanbao == human.db.investRMB and InvestConfig[i].index == condition then
      if human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + condition - 1] ~= FuliDefine.REWARD_CAN_GET then
        return
      end
      if InvestConfig[i].needLv > human.db.lv then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_LV_NOT_ENOUGH)
      end
      if not BagLogic.checkCanAddItemList(human, InvestConfig[i].reward,#InvestConfig[i].reward) then
        return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)
      end
      human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + condition - 1] = FuliDefine.REWARD_ALREADY_GET
      for j=1, #InvestConfig[i].reward do
        BagLogic.addItem(human, InvestConfig[i].reward[j][1], 1, InvestConfig[i].reward[j][2], "fulidating")
      end      
      break
    end
  end
  getRewardList(human)
  Handler.checkShowTips(human)
end
--检查奖励是否领取完
function checkGetAll(human)
  for i=1, #InvestConfig do
    if human.db.investRMB == 0 or human.db.investRMB ~= 0 and InvestConfig[i].needYuanbao == human.db.investRMB and human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + InvestConfig[i].index - 1] ~= FuliDefine.REWARD_ALREADY_GET then
      return false
    end
  end
  return true
end
--等级改变的时候改变状态
function changeStatus(human)
  if human.db.investRMB == 0 then
    return
  end
  for i=1, #InvestConfig do
    if InvestConfig[i].needYuanbao == human.db.investRMB and human.db.lv >= InvestConfig[i].needLv
      and human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + InvestConfig[i].index - 1] == FuliDefine.REWARD_NOT_CAN_GET then
      human.db.fuliIsGet[FuliDefine.FULI_TYPE_INVEST * 15 + InvestConfig[i].index - 1] = FuliDefine.REWARD_CAN_GET
    end
  end
  Handler.checkShowTips(human)
end
--投资
function invest(human, rmb)
  if human.db.investRMB ~= 0 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_ALREADY_INVEST) 
  end
  for i=1, #InvestConfig do
    if InvestConfig[i].needYuanbao == rmb then
      if not ObjHuman.checkYuanbaoEnough(human, InvestConfig[i].needYuanbao) then
        ShopHelp.onNoRMB(human)
        return false
      end
      ObjHuman.decYuanbao(human, InvestConfig[i].needYuanbao, "fulidating")
      human.db.investRMB = rmb
      Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_INVEST_SUCCESS)
      break      
    end
  end
  changeStatus(human)
  Handler.checkShowTips(human)
  Log.write(Log.LOGID_OSS_INVEST, human._id, human.account, human.name,human.db.lv)
  return false
end
--等级变更
function onLvUp(human)
  changeStatus(human)
end